package czh.cocoa.graphics;

import android.graphics.Rect;
import android.graphics.RectF;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSString;
import czh.cocoa.ui.UIEdgeInsets;

public class CGRect extends NSObject {

	public CGPoint origin;
	
	public CGSize size;
	
	public CGRect() {
		this(0,0,0,0);
	}
	
	public CGRect(float x, float y, float width, float height) {
		origin = new CGPoint(x, y);
		size = new CGSize(width, height);
	}
	
	public CGRect(CGRect rect) {
		this(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); 
	}

	public CGRect(Rect rect) {
		this(rect.left, rect.top, rect.width(), rect.height());
	}
	
	public CGRect(RectF rect) {
		this(rect.left, rect.top, rect.width(), rect.height());
	}
	
	public CGRect clone() {
		return new CGRect(origin.x, origin.y, size.width, size.height); 
	}
	
	public boolean isEqual(NSObject o) {
		if(o instanceof CGRect) {
			CGRect other = (CGRect) o;
			return origin.isEqual(other.origin) && size.isEqual(other.size);
		}
		return false;
	}
	
	public boolean containsPoint(CGPoint point) {
		return point.x >= origin.x && point.x <= origin.x + size.width &&
		       point.y >= origin.y && point.y <= origin.y + size.height;
	}
	
	public NSString description() {
		return NSTR("[" + origin.x + ", " + origin.y + ", " + size.width + ", " + size.height +  "]");
	}
	
	public CGRect union(CGRect other) {
		float left = Math.min(origin.x, other.origin.x);
		float top = Math.min(origin.y, other.origin.y);
		float right = Math.max(origin.x + size.width, other.origin.x + other.size.width);
		float bottom = Math.max(origin.y + size.height, other.origin.y + other.size.height);
		return CGRectMake(left, top, right - left, bottom - top);
	}
	
	public CGRect intersect(CGRect other) {
		float left = origin.x;
		float right = origin.x + size.width;
		float top = origin.y;
		float bottom = origin.y + size.height;
		
		float left2 = other.origin.x;
		float right2 = other.origin.x + other.size.width;
		float top2 = other.origin.y;
		float bottom2 = other.origin.y + other.size.height;
		
		if (left < right2 && left2 < right && top < bottom2 && top2 < bottom) {
            if (left < left2) {
                left = left2;
            }
            if (top < top2) {
                top = top2;
            }
            if (right > right2) {
                right = right2;
            }
            if (bottom > bottom2) {
                bottom = bottom2;
            }
            return new CGRect(left, top, right - left, bottom - top); 
        }
		
        return null;
	}
	
	public CGRect inset(float dx, float dy) {
		CGRect rect = new CGRect(this);
		rect.origin.x += dx;
		rect.origin.y += dy;
		rect.size.width -= dx * 2;
		rect.size.height -= dy * 2;
		return rect;
	}
	
	public CGRect inset(UIEdgeInsets insets) {
		CGRect rect = new CGRect(this);
		rect.origin.x += insets.left;
		rect.origin.y += insets.top;
		rect.size.width -= (insets.left + insets.right);
		rect.size.height -= (insets.top + insets.bottom);
		return rect;
	}
	
	public Rect toRect() {
		int left = (int)origin.x;
		int top = (int)origin.y;
		int right = (int)(origin.x + size.width);
		int bottom = (int)(origin.y + size.height);
		
		return new Rect(left, top, right, bottom);  
	}
	
	public RectF toRectF() {
		float left = origin.x;
		float top =  origin.y;
		float right = origin.x + size.width;
		float bottom = origin.y + size.height;
		
		return new RectF(left, top, right, bottom);  
	}
	
	public CGPoint center() {
		return CGPointMake(origin.x + size.width / 2, origin.y + size.height / 2); 
	}
	
}
